package com.ruoyi.reception;

import com.ruoyi.common.constant.RedisConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.receipt.service.ALSmsSendService;
import com.ruoyi.receipt.utils.CompOutSubjectUtils;
import com.ruoyi.receipt.utils.FormulaUtil;
import com.ruoyi.receipt.utils.StatisAnalysisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2021-03-15
 */
@RestController
@RequestMapping("/reception/aLSmsSend")
public class ALSmsSendController extends BaseController
{
    @Autowired
    private ALSmsSendService aLSmsSendService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private CompOutSubjectUtils compOutSubjectUtils;

    @Autowired
    private StatisAnalysisUtils statisAnalysisUtils;

    @Autowired
    private FormulaUtil formulaUtil;

    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 发送短信注册码
     */
    @PostMapping("/sendVerCode")
    public AjaxResult sendVerCode(@RequestBody Map<String,String> map)
    {
        String phone=map.get("phone");
        aLSmsSendService.sendVerCode(phone);
        return AjaxResult.success("验证码发送成功");
    }

    /**
     * 发送修改手机号验证码
     */
    @PostMapping("/sendUpdatePhoneCode")
    public AjaxResult updatePhoneCode(@RequestBody Map<String,String> map)
    {
        String phone=map.get("phone");
        aLSmsSendService.sendUpdatePhoneCode(phone);
        return AjaxResult.success("验证码发送成功");
    }

    //my test===========================================================================================================================

    /**
     * 测试出例题
     */
    @PostMapping("/testcCompOutSubject")
    public AjaxResult testComp() {

        List<Long> clusteIdList=new ArrayList<>();
        clusteIdList.add(7091L);
        clusteIdList.add(8092L);
        clusteIdList.add(5602L);
        List<Long> longs = compOutSubjectUtils.compOutSubject(123L, clusteIdList, 3);
        for (int i = 0; i <longs.size() ; i++) {
            System.out.println(longs.get(i));
        }

        return AjaxResult.success();
    }


    /**
     * 初始化测试题簇
     */
    @PostMapping("/initClustData")
    public AjaxResult initClustData() {

        //初始化第一组题簇
        Long  clusteId=7091L;
        //系统中的总记录本
        List<Long> subjectIdList=new ArrayList<>();
        subjectIdList.add(70911111L);
        subjectIdList.add(70912222L);
        subjectIdList.add(70913333L);
        subjectIdList.add(70914444L);
        redisCache.bathZsetByListScoreRandom(RedisConstants.CLUSTE_SUBJEC_RELATION+clusteId,subjectIdList);


        //初始化第二组题簇
        Long  clusteId2=8092L;
        //系统中的总记录本
        List<Long> subjectIdList2=new ArrayList<>();
        subjectIdList2.add(80921111L);
        subjectIdList2.add(80922222L);
        subjectIdList2.add(80923333L);
        subjectIdList2.add(80924444L);
        redisCache.bathZsetByListScoreRandom(RedisConstants.CLUSTE_SUBJEC_RELATION+clusteId2,subjectIdList2);


        //初始化第三组题簇
        Long  clusteId3=5602L;
        //系统中的总记录本
        List<Long> subjectIdList3=new ArrayList<>();
        subjectIdList3.add(56021111L);
        subjectIdList3.add(56022222L);
        subjectIdList3.add(56023333L);
        subjectIdList3.add(56024444L);
        redisCache.bathZsetByListScoreRandom(RedisConstants.CLUSTE_SUBJEC_RELATION+clusteId3,subjectIdList3);

        return AjaxResult.success();
    }


    /**
     * test
     */
    @PostMapping("/test")
    public AjaxResult test()
    {
//        redisTemplate.opsForZSet().add("t2",5, 90);
//        Double typedTupleSet=redisTemplate.opsForZSet().score("t2", 5);
//        Double typedTupleSet2 = redisCache.getZsetScore("today:cut:out:time:2021-03-31", 18253100476L);
//        System.out.println("typedTupleSet==>"+typedTupleSet);
//        System.out.println("typedTupleSet2==>"+typedTupleSet2);
/*
        //推入今天这个用户做的所有题目
        List<Long> allSubject=new ArrayList<>();
        allSubject.add(123L);
        allSubject.add(456L);
        allSubject.add(789L);
        statisAnalysisUtils.putTodayAllSubject(18253100476L,allSubject);

        //推入今天这个用户做的所有题目
        List<Long> wrongSubject=new ArrayList<>();
        wrongSubject.add(456L);
        statisAnalysisUtils.putTodayWrongSubject(18253100476L,wrongSubject);

        //推入今天这个用户的学习时长,自动进行累加
        statisAnalysisUtils.putTodayLearnTime(18253100476L,25);

        //推入今天这个用户的切出次数,调一次自动加1
        statisAnalysisUtils.putTodayCutOutNumber(18253100476L);

        //推入今天这个用户的切出次数,调一次自动加1
        statisAnalysisUtils.putTodayCutOutTime(18253100476L,15);

        //推入难度大于35分的题目
        statisAnalysisUtils.putDiffictRatioGreater35(456L);
*/

   /*

      /* Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("username",new WeiXinMsgData("张三","#FF0000"));
        dataMap.put("address",new WeiXinMsgData("天安门","#FF0000"));
        dataMap.put("money",new WeiXinMsgData("18.5","#FF0000"));

        weiXinMsg.sendMag(dataMap,"123","ozadk0nBhLjGQHxM1GClfq_FgkAA");*/

     /*   redisTemplate.opsForZSet().add("t1",1, 80);
        redisTemplate.opsForZSet().add("t1",2, 50);
        redisTemplate.opsForZSet().add("t1",3, 40);

        redisTemplate.opsForZSet().add("t2",5, 90);
        redisTemplate.opsForZSet().add("t2",4, 1000);
        redisTemplate.opsForZSet().add("t2",6, 3500);*/

/*        long startTime = System.currentTimeMillis();
        Set<Long> set = redisCache.zSetByReversePage("typedTupleSet",70,10);
        for (Long str : set) {
            System.out.println("第70页数据==>"+str);
        }

        Set<Long> sett =  redisCache.zSetByReversePage("typedTupleSet",80,10);

        for (Long str : sett) {
            System.out.println("第80页数据==>"+str);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("消耗时间===>"+(endTime-startTime));*/

/*     for (int i = 0; i < 50000; i++) {
            System.out.println(i);
            Map<Long,Double> map=new HashMap<>();
            Integer random=((int)((Math.random()*9+1)*1000));
            map.put(idWorker.nextId(),Double.parseDouble(random+""));
            redisCache.zSetBatchInsert("cuoti",map);
            redisCache.zSetBatchInsert("anli",map);

            if(random%23==0){
                redisCache.zSetBatchInsert("cuotiji",map);
            }

             if(random%77==0){
                 redisCache.zSetBatchInsert("anliji",map);
             }
     }*/
  /*        map.put(idWorker.nextId(),Double.parseDouble(((int)((Math.random()*9+1)*1000))+""));
          redisCache.zSetBatchInsert("rt2",map);*/

        //long startTime = System.currentTimeMillis();
       /* redisCache.zSetIntersection("anli","anliji","anliji_ji");
        redisCache.zSetIntersection("cuoti","cuotiji","cuoti_ji");
        redisCache.zSetIntersection("anliji_ji","cuoti_ji","res_ji");
        redisTemplate.opsForSet().differenceAndStore("cuotiji","cuoti","cc");
        long endTime = System.currentTimeMillis();

        System.out.println("消耗时间===>"+(endTime-startTime));*/

        // 1.executePipelined 重写 入参 RedisCallback 的doInRedis方法
 /*       List<Object> resultList = redisTemplate.executePipelined(new RedisCallback<Object>() {

            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                // 2.connection 打开管道
                connection.openPipeline();
                // 3.connection 给本次管道内添加 要一次性执行的多条命令
                for (int i = 0; i < 50000; ++i) {
                    connection.zAdd(("cuotiben").getBytes(),i, (idWorker.nextId()+"").getBytes());
                }
                // 4.关闭管道 不需要close 否则拿不到返回值
                // connection.closePipeline();
                // 这里一定要返回null，最终pipeline的执行结果，才会返回给最外层
                return null;
            }
        });*/

  /*      Long start = System.currentTimeMillis();
        Long start1 = System.currentTimeMillis();
        Long end = System.currentTimeMillis();
        System.out.println("---" + (start1 - end));
        // 5.最后对redis pipeline管道操作返回结果进行判断和业务补偿
        for (Object object : resultList) {
            System.out.println(object);
        }*/

        //redisTemplate.opsForZSet().incrementScore("typedTupleSet2",1,2);
        //redisTemplate.opsForZSet().intersectAndStore("t1","t2","t3");

        //redisCache.zSetunionAndStore("rt1","rt2","rt3");
        //SINTERSTORE userid:new userid:20002 userid:20003
   /*     long startTime = System.currentTimeMillis();
        for(int i=0;i<20;i++){
            Double typedTupleSet = redisTemplate.opsForZSet().score("typedTupleSet", 1373124455752900608L);
            //System.out.println(typedTupleSet);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("消耗时间===>"+(endTime-startTime));
        */
        return AjaxResult.success();
    }

}
